package com.itheima.user.controller.impl;

import com.itheima.entry.Entry;
import com.itheima.user.controller.IUserController;
import com.itheima.user.service.IUserService;
import com.itheima.util.EmailUtil;
import com.itheima.domain.User;
import com.itheima.factory.Factory;

import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserControllerImpl implements IUserController {
    IUserService iUserService;

    {
        try {
            iUserService = Factory.getIUserService();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Scanner sc = new Scanner(System.in);



    @Override
    public void start(String username) throws Exception {
        while (true) {
            System.out.println("请输入您的选择1.修改密码  2.修改邮箱  3.注销账号  4.查看账号信息  5.退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    updatePassword(username);
                    break;
                case "2":
                    updateEmail(username);
                    break;
                case "3":
                    if (deleteUser(username)) {
                        System.out.println("注销成功");
                        write();
                        Entry.entryMethod();
                        return;
                    }
                    break;
                case "4":
                    findUser(username);
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;

            }
        }
    }

    @Override
    public void addUser() throws Exception {
        System.out.println("请输入账号");
        String username = sc.next();
        if (iUserService.isExists(username)) {
            System.out.println("账号已存在");
            return;
        }
        System.out.println("请输入密码");
        String password = sc.next();
        System.out.println("请确认密码");
        String password1 = sc.next();
        if (password.equals(password1)) {
            while (true) {
                System.out.println("请输入邮箱(邮箱格式:123456@163.com)");
                String email = sc.next();
                if (email.contains("@") && email.endsWith(".com")) {
                    if (iUserService.isExistsEmail(email)) {
                        System.out.println("邮箱已被注册");
                        return;
                    } else {
                        StringBuilder sb = new StringBuilder();
                        Random ra = new Random();
                        String[] s = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"};
                        for (int i = 0; i < 6; i++) {
                            int num = ra.nextInt(62);
                            sb = sb.append(s[num]);
                        }
                        EmailUtil.sendMail(email, sb.toString(), "验证码");
                        System.out.println("请输入" + email + "收到的验证码");
                        String newrandom = sc.next();
                        if (newrandom.equals(sb.toString())) {
                            System.out.println("注册成功,请查看邮箱");
                            EmailUtil.sendMail(email, "账号"+username+"密码"+password, "恭喜注册成功");
                            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                            //存注册时间
                            LocalDateTime time = LocalDateTime.now();
                            String data = time.format(pattern);
                            String zhuceTime = "用户" + username + "于" + data + "注册成功";
                            FileWriter fw = new FileWriter("system\\message\\time\\zhuceTime.txt",true);
                            fw.write(zhuceTime);
                            fw.write("\r\n");
                            fw.close();
                            User user = new User(username, password, email);
                            iUserService.addUser(user);
                            //start(username);//
                            //Entry.choose(username);
                            break;
                        }else {
                            System.out.println("验证码有误");
                            return;
                        }
                    }
                } else {
                    System.out.println("邮箱格式有误,请重新输入");
                }
            }
        } else {
            System.out.println("两次密码不一致");
        }
    }

    @Override
    public void updatePassword(String username) {
        String email = getEmail(username);
        String random = random(username);
        for (int i = 0; i < 3; i++) {
            int count = 2;
            System.out.println("请输入" + email + "收到的验证码(还有" + (count - i) + "次机会)");
            String newrandom = sc.next();
            if (newrandom.equals(random)) {
                System.out.println("验证成功,请输入新密码");
                String password = sc.next();
                iUserService.updatePassword(username, new User(username, password, email));
                System.out.println("修改成功,请查看邮箱");
                EmailUtil.sendMail(email, "账号"+username+"密码"+password, "您的密码已经修改成功");
                break;
            } else {
                System.out.println("验证码错误,请重新输入");
            }
        }
    }

    @Override
    public void updateEmail(String username) {
        ArrayList<User> list = iUserService.findAllUser();
        String email = getEmail(username);
        String random = random(username);
        for (int i = 0; i < 3; i++) {
            int count = 2;
            System.out.println("请输入" + email + "收到的验证码(还有" + (count - i) + "次机会)");
            String newrandom = sc.next();
            if (newrandom.equals(random)) {
                while (true) {
                    System.out.println("验证成功,请输入新邮箱(邮箱格式:123456@163.com)");
                    String newemail = sc.next();
                    if (newemail.contains("@") && newemail.endsWith(".com")) {
                        break;
                    } else {
                        System.out.println("邮箱格式有误,请重新输入");
                    }
                }
                for (User user1 : list) {
                    if (user1.getUsername().equals(username)) {
                        user1.setEmail(email);
                    }
                }
                System.out.println("修改成功");
                EmailUtil.sendMail(email, "账号"+username+"解除绑定您的邮箱", "账户邮箱已经修改");
                break;
            } else {
                System.out.println("验证码错误");
            }
        }
    }

    @Override
    public ArrayList<User> findAllUser() {
        return iUserService.findAllUser();
    }

    @Override
    public boolean deleteUser(String username) {
        String email = getEmail(username);
        String random = random(username);
        for (int i = 0; i < 3; i++) {
            int count = 2;
            System.out.println("请输入" + email + "收到的验证码(还有" + (count - i) + "次机会)");
            String newrandom = sc.next();
            if (newrandom.equals(random)) {
                iUserService.deleteUser(username);
                return true;
            } else {
                System.out.println("验证码错误,请重新输入");
            }
        }
        return false;
    }

    @Override
    public void findUser(String username) {
        User user = iUserService.findUser(username);
        System.out.println("账号\t密码\t邮箱");
        System.out.println(username + "\t" + user.getPassword() + "\t" + user.getEmail());
    }

    @Override
    public String getEmail(String username) {
        ArrayList<User> list = iUserService.findAllUser();
        String email = null;
        for (User user : list) {
            if (user.getUsername().equals(username)) {
                email = user.getEmail();
            }
        }
        return email;
    }

    @Override
    public void write() throws IOException {
        iUserService.write();
    }

    @Override
    public void read() throws IOException, ClassNotFoundException {
        iUserService.read();
    }

    @Override
    public String random(String username) {

        String email = getEmail(username);
        StringBuilder sb = new StringBuilder();
        Random ra = new Random();
        String[] s = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"};
        for (int i = 0; i < 6; i++) {
            int num = ra.nextInt(62);
            sb = sb.append(s[num]);
        }
        EmailUtil.sendMail(email, sb.toString(), "验证码");
        return sb.toString();
    }


}
